﻿unit Controller.User;

interface

uses
  System.SysUtils,
  System.Classes,
  MVC.Command,
  xsuperobject,
  MVC.View,
  MVC.BaseController,
  Controller.Custom,
  MVC.Rtti.Attribute,
  qjson;

type
  [PathAttrib('/User', 'User', false)]
  TUsersController= class(TCustomController)
  public
    procedure Index();
    [mvcHttpPost('/login')]
    procedure Login();
    procedure Logout();
    //刷新token
    procedure refesh();
    procedure forget();
    procedure Reg();
    procedure Add();
    procedure Edit(id: Integer);
    procedure list();
    procedure Savedata();
    //用户信息
    procedure getUserInfo();
    //水印
    procedure VerifyCode();
    //左边菜单
    procedure getMenu(index: Integer);
    procedure getTopMenu();
  end;

implementation

{ TUsersController }

uses
  uTableMap,
  MVC.LogUnit,
  MVC.ORM,
  myCommand;

procedure TUsersController.Add;
begin
  with View do
  begin
    ShowHTML('add');
  end;
end;

procedure TUsersController.Edit(id: Integer);
var
  sJson: string;
begin
  with View do
  begin
    sjson := Db.Default.FindFirst(tb_users, 'id=' + id.ToString).asjson;
    setAttr('user', sjson);
    ShowHTML('edit');
  end;
end;

procedure TUsersController.Index;
var
  sdata: ISuperObject;
begin
  with View do
  begin
    sdata := Db.Default.Find(tb_users, 'limit 10');
    setAttr('sdata', sdata.AsJSON);
    ShowHTML('index');
  end;
end;

procedure TUsersController.list;
var
  s: string;
begin
  with View do
  begin
    s := Request.Query;
    ShowHTML('list');
  end;
end;

procedure TUsersController.Login;
var
  sUserName: string;
  sPassword: string;
  sCode: string;
  sRedomStr: string;
  aJson: ISuperObject;
  aEmp: ISuperObject;
begin
//View.Request.Content = {"username":"admin","password":"U2FsdGVkX1+ui23B0V0yC71gbBT5d10yNijioZ7hXeQ=","code":"9526","redomStr":"31311622775143669"}
  aJson := SO(View.Request.Content);
  if aJson = nil then
  begin
    View.Fail(-1, '格式错误');
    Exit;
  end;
  sUserName := aJson.S['username'];
  sPassword := aJson.S['password']; //加密后的密码
  sCode     := aJson.S['code'];     //验证码
  sRedomStr := aJson.S['redomstr'];
  View.SessionSet('UserName', sUserName);
  sPassword := Base64Decode(sPassword);
  var sSQL: string;
  sSQL := 'select id,usercode,"password" as fpassword from employe where usercode=' + sUserName.QuotedString;
  aEmp := View.Db.Default.QueryFirst(sSQL);
  if aEmp = nil then
    View.Fail(403, '帐号错误')
  else
  begin
     if sPassword = aEmp.S['fpassword'] then
        View.Success(200, '登陆成功')
     else
     begin
        View.Fail(403, '密码错误');
     end;
  end;
end;

procedure TUsersController.Logout();
begin
    view.SessionSet('UserName', '');
    view.SessionSet('Password', '');
    View.Success(200, '已经退出');
end;

procedure TUsersController.refesh;
begin
  view.ShowText('xxxxxxxxxx');
end;

procedure TUsersController.forget();
begin
  view.ShowHTML('forget');
end;

procedure TUsersController.getTopMenu;
begin
    View.ShowFile(AppWebRoot + 'Model/user/top.json');
end;

procedure TUsersController.getUserInfo;
begin
    view.ShowFile(AppWebRoot + 'Model/user/getUserInfo.json');
end;

procedure TUsersController.getMenu(index: Integer);
begin
  log('getMenu:' + view.Request.Content);
  View.ShowFile(AppWebRoot + 'Model/user/first.json');
end;

procedure TUsersController.Reg();
begin
   view.ShowHTML('Register');
end;

procedure TUsersController.Savedata;
var
  userid: string;
  ret: ISuperObject;
  tmpCds: TmvcQuery;
begin
  ret := SO();
  with View do
  begin
    userid := Input('id');
    if (userid = '') then
      tmpCds := Db.Default.AddData(tb_users)
    else
      tmpCds := Db.Default.EditData(tb_users, 'id' ,userid);
    try
      tmpCds.FieldByName('name').AsString     := Input('name');
      tmpCds.FieldByName('username').AsString := Input('username');
      tmpCds.FieldByName('phone').AsString    := Input('phone');
      tmpCds.FieldByName('age').AsString      := Input('age');
      tmpCds.FieldByName('sex').AsString      := Input('sex');
      tmpCds.FieldByName('address').AsString  := Input('address');
      tmpCds.FieldByName('idcard').AsString   := Input('idcard');
      tmpCds.FieldByName('phone').AsString    := Input('phone');
      tmpCds.Post;
      ret.I['code'] := 0;
      ret.S['message'] := '保存成功';
    except
      ret.I['code'] := -1;
      ret.S['message'] := '保存失败';
    end;
    ShowJson(ret);
  end;

end;


procedure TUsersController.VerifyCode;
var
  sNum: string;
begin
  sNum := View.Input('vCode');
  sNum := Copy(sNum, 1, 4);
  if sNum = '' then
  begin
    Randomize;
    sNum := inttostr(Random(9)) + inttostr(Random(9)) + inttostr(Random(9)) + inttostr(Random(9));
  end;
  View.SessionSet('VerifyCode', sNum);
//  View.VerifyCode(sNum);
end;

initialization
    RegisterClass(TUsersController);
//  RegisterRoule('/User',  TUsersController,   'user');
finalization
//  UnRegisterRoule('/User');
    UnRegisterClass(TUsersController);

end.

